<script setup lang="ts">
import { avVideo } from "./utils/hook";
import { ref, computed } from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import {
  isString,
  cloneDeep,
  isAllEmpty,
  intersection,
  storageLocal,
  isIncludeAllChildren
} from "@pureadmin/utils";
import {
  delay,
  subBefore,
  deviceDetection,
  useResizeObserver
} from "@pureadmin/utils";

// import Database from "~icons/ri/database-2-line";
// import More from "~icons/ep/more-filled";
import Delete from "~icons/ep/delete";
import EditPen from "~icons/ep/edit-pen";
import Refresh from "~icons/ep/refresh";
import Search from "~icons/ep/search";
import Download from "~icons/ep/download";
import Upload from "~icons/ep/upload";
import Circle from "~icons/ep/circle-check";
import Menu from "~icons/ep/menu";
import AddFill from "~icons/ri/add-circle-line";
import Close from "~icons/ep/close";
import Sort from "~icons/ep/sort";

defineOptions({
  name: "JaVideoList"
});

const iconClass = computed(() => {
  return [
    "w-[22px]",
    "h-[22px]",
    "flex",
    "justify-center",
    "items-center",
    "outline-hidden",
    "rounded-[4px]",
    "cursor-pointer",
    "transition-colors",
    "hover:bg-[#0000000f]",
    "dark:hover:bg-[#ffffff1f]",
    "dark:hover:text-[#ffffffd9]"
  ];
});

const treeRef = ref();
const formRef = ref();
const tableRef = ref();
const contentRef = ref();
const treeHeight = ref();

const {
  form,
  isShow,
  curRow,
  loading,
  columns,
  rowStyle,
  dataList,
  // treeData,
  treeProps,
  isLinkage,
  pagination,
  // isExpandAll,
  // isSelectAll,
  treeSearchValue,
  showHistory,
  searchHistory,
  onSearch,
  getSearch,
  handleSearch,
  selectHistory,
  removeHistory,
  clearHistory,
  handleBlur,
  expandHistory,
  avGain,
  avRefresh,
  avIDRefresh,
  addExist,
  downPreview,
  batchDown,
  resetForm,
  openDialog,
  // handleSave,
  handleDelete,
  // onQueryChanged,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange
} = avVideo(treeRef);

const centerDialogVisible = ref(false);

// 父级选项数据
const parentOptions = [
  { value: "khyy", label: "khyy" },
  { value: "hsck", label: "hsck" },
  { value: "xxav", label: "xxav" },
  { value: "ooxx", label: "ooxx" },
  { value: "thep", label: "thep" },
  { value: "kedou", label: "kedou" },
  { value: "91porny", label: "91porny" },
  { value: "madouqu", label: "madouqu" },
  { value: "papapa", label: "papapa" },
  { value: "mienav", label: "mienav" },
  { value: "4Se", label: "4Se" },
  { value: "dogB", label: "dogB" },
  { value: "8mav", label: "8mav" },
  { value: "psd", label: "psd" },
  { value: "91porny", label: "91porny" },
  { value: "66m", label: "66m" },
  { value: "9se", label: "9se" },
  { value: "youporn", label: "youporn" },
  { value: "hiporn", label: "hiporn" },
  { value: "xyshu", label: "xyshu" },
  { value: "deepfakeporn", label: "deepfakeporn" }
];

// 子级选项映射表
const childOptionsMap = {
  khyy: [
    { value: "apple", label: "苹果" },
    { value: "banana", label: "香蕉" },
    { value: "orange", label: "橙子" }
  ],
  hsck: [
    { value: "tomato", label: "番茄" },
    { value: "potato", label: "土豆" },
    { value: "cucumber", label: "黄瓜" }
  ],
  xxav: [
    { value: "beef", label: "牛肉" },
    { value: "pork", label: "猪肉" },
    { value: "chicken", label: "鸡肉" }
  ]
};

const parentValue = ref("");
const childValue = ref("");
const childOptions = ref([]);

// 处理父级选择变化
const handleParentChange = val => {
  childValue.value = ""; // 清空子级选择
  childOptions.value = val ? childOptionsMap[val] : [];
};
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[6px]"
      @keyup.enter="onSearch"
    >
      <el-form-item label="名称" prop="title">
        <el-input
          v-model="form.title"
          placeholder="请输入名称"
          clearable
          size="small"
          class="!w-[240px]"
          focus="true"
          @focus="showHistory = true"
          @blur="handleBlur"
          @keyup.enter="handleSearch"
        />
        <el-dropdown
          v-model="showHistory"
          trigger="click"
          placement="bottom-start"
          class="history-dropdown"
        >
          <div
            v-show="showHistory && searchHistory.length > 0"
            class="history-panel"
          >
            <div class="history-header">
              <span>搜索历史</span>
              <span>
                <!-- 展开历史记录 -->
                <el-button
                  v-if="searchHistory.length > 0"
                  type="primary"
                  size="small"
                  :icon="useRenderIcon(Sort)"
                  @click="expandHistory"
                />
                <!-- 清空历史记录 -->
                <el-button
                  v-if="searchHistory.length > 0"
                  type="danger"
                  size="small"
                  :icon="useRenderIcon(Delete)"
                  @click="clearHistory"
                />
              </span>
            </div>
            <el-scrollbar max-height="200px">
              <div
                v-for="(item, index) in searchHistory"
                :key="index"
                class="history-item"
                @click="selectHistory(item)"
              >
                {{ item }}
                <el-icon class="delete-icon" @click.stop="removeHistory(index)">
                  <Close />
                </el-icon>
              </div>
            </el-scrollbar>
          </div>
        </el-dropdown>
      </el-form-item>
      <el-form-item label="番号" prop="code">
        <el-input
          v-model="form.code"
          placeholder="请输入番号"
          clearable
          size="small"
          class="!w-[100px]"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="form.status"
          placeholder="请选择状态"
          clearable
          size="small"
          class="!w-[100px]"
        >
          <el-option label="已存在" value="1" />
          <el-option label="不存在" value="0" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="站点" prop="siteClass">
        <el-select
          v-model="form.siteClass"
          placeholder="请选择站点"
          clearable
          size="small"
          class="!w-[100px]"
        >
          <el-option label="khyy" value="khyy" />
          <el-option label="hsck" value="hsck" />
          <el-option label="xxav" value="xxav" />
          <el-option label="ooxx" value="ooxx" />
          <el-option label="thep" value="thep" />
          <el-option label="kedou" value="kd" />
          <el-option label="91porny" value="91porny" />
          <el-option label="madouqu" value="madouqu" />
          <el-option label="papapa" value="papapa" />
          <el-option label="mienav" value="mienav" />
          <el-option label="4Se" value="4Se" />
          <el-option label="dogB" value="dogB" />
          <el-option label="8mav" value="8mav" />
          <el-option label="psd" value="psd" />
          <el-option label="91porny" value="91porny" />
          <el-option label="66m" value="66m" />
          <el-option label="9se" value="9se" />
          <el-option label="youporn" value="youporn" />
          <el-option label="hiporn" value="hiporn" />
          <el-option label="xyshu" value="xyshu" />
          <el-option label="deepfakeporn" value="deepfakeporn" />
        </el-select>
      </el-form-item>
      <el-form-item label="系列" prop="series">
        <el-select
          v-model="form.series"
          placeholder="请选择系列"
          clearable
          size="small"
          class="!w-[100px]"
        >
          <el-option label="khyy" value="khyy" />
        </el-select>
      </el-form-item> -->

      <el-form-item label="站点">
        <el-select
          v-model="form.siteClass"
          placeholder="请选择站点"
          clearable
          size="small"
          class="!w-[100px]"
          @change="handleParentChange"
        >
          <el-option
            v-for="item in parentOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="类别">
        <el-select
          v-model="form.series"
          placeholder="请先选择类别"
          :disabled="!form.siteClass"
          size="small"
          clearable
          class="!w-[100px]"
        >
          <el-option
            v-for="item in childOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          size="small"
          @click="onSearch"
        />
        <el-button
          :icon="useRenderIcon(Refresh)"
          size="small"
          @click="resetForm(formRef)"
        />
      </el-form-item>
    </el-form>

    <div
      ref="contentRef"
      :class="['flex', deviceDetection() ? 'flex-wrap' : '']"
    >
      <PureTableBar title="视频列表" :columns="columns" @refresh="onSearch">
        <template #buttons>
          <el-button
            type="danger"
            size="small"
            :icon="useRenderIcon(AddFill)"
            @click="openDialog('新增')"
          >
            新增视频
          </el-button>
          <el-button
            type="primary"
            size="small"
            :icon="useRenderIcon(Circle)"
            @click="addExist('已选')"
          >
            设置已选
          </el-button>
          <el-button
            type="warning"
            size="small"
            :icon="useRenderIcon(Download)"
            @click="batchDown('local')"
          >
            批量本地
          </el-button>
          <el-button
            type="success"
            size="small"
            :icon="useRenderIcon(Upload)"
            @click="batchDown('remoto')"
          >
            批量远程
          </el-button>
          <el-button
            size="small"
            color="#626aef"
            :icon="useRenderIcon(Upload)"
            @click="batchDown('tower')"
          >
            批量远程
          </el-button>
          <el-button
            size="small"
            color="#72557D"
            :icon="useRenderIcon(Circle)"
            @click="downPreview('已选')"
          >
            下载预览
          </el-button>
          <el-button
            size="small"
            color="#029A8D"
            :icon="useRenderIcon(Refresh)"
            @click="avRefresh(dataList)"
          >
            刷新存在
          </el-button>
        </template>
        <template v-slot="{ size, dynamicColumns }">
          <pure-table
            align-whole="center"
            header-align="center"
            height="1040"
            showOverflowTooltip
            table-layout="auto"
            :loading="loading"
            size="small"
            adaptive
            :adaptiveConfig="{ offsetBottom: 76 }"
            :data="dataList"
            :columns="dynamicColumns"
            :pagination="pagination"
            :paginationSmall="size === 'small' ? true : false"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @selection-change="handleSelectionChange"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <div class="flex flex-wrap justify-end">
                <el-button
                  type="warning"
                  size="small"
                  :icon="useRenderIcon(Download)"
                  @click="avGain(row, 'local')"
                />
                <el-button
                  type="success"
                  size="small"
                  :icon="useRenderIcon(Upload)"
                  @click="avGain(row, 'remoto')"
                />
                <el-button
                  size="small"
                  color="#626aef"
                  :icon="useRenderIcon(Upload)"
                  @click="avGain(row, 'tower')"
                />

                <el-button
                  type="primary"
                  size="small"
                  :icon="useRenderIcon(Refresh)"
                  @click="avIDRefresh(row)"
                />
                <el-button
                  type="info"
                  size="small"
                  :icon="useRenderIcon(EditPen)"
                  @click="openDialog('修改', row)"
                />
                <el-popconfirm
                  :title="`确认删除${row.av_id}`"
                  @confirm="handleDelete(row)"
                >
                  <template #reference>
                    <el-button
                      type="danger"
                      size="small"
                      :icon="useRenderIcon(Delete)"
                    />
                  </template>
                </el-popconfirm>
              </div>
            </template>
          </pure-table>
          <el-dialog
            v-model="centerDialogVisible"
            title="Warning"
            width="500"
            align-center
          >
            <span>Open the dialog from the center from the screen</span>
            <template #footer>
              <div class="dialog-footer">
                <el-button @click="centerDialogVisible = false"
                  >Cancel</el-button
                >
                <el-button type="primary" @click="centerDialogVisible = false">
                  Confirm
                </el-button>
              </div>
            </template>
          </el-dialog>
        </template>
      </PureTableBar>
    </div>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
    margin-right: 15px;
  }
}

.main-content {
  margin: 16px 10px 0 !important;
}

.el-button--small {
  margin-bottom: 4px !important;
}

// :deep(.el-table__body) {
//   height: 1036px !important;
// }

// :deep(.el-table__row) {
//   height: 67px !important;
// }

.search-container {
  position: relative;
  width: 300px;
}

.history-dropdown {
  position: absolute;
  width: 100%;
  top: 30px;
  z-index: 1000;
}

.history-panel {
  width: 100%;
  background: white;
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-top: 5px;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 10px;
  border-bottom: 1px solid #f0f0f0;
  color: #909399;
  font-size: 12px;
}

.history-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  // padding: 5px 10px;
  border-bottom: 1px solid #f0f0f0;
  color: #909399;
  font-size: 12px;
}

.history-item {
  padding: 8px 10px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.history-item:hover {
  background-color: #f5f7fa;
}

.delete-icon {
  color: #c0c4cc;
}

.delete-icon:hover {
  color: #f56c6c;
}
</style>
